Debian Codegraph @

www/src/pages/[...slug].md.js

raw symbols SCIP
1 import { getFolder, fileAsMarkdown } from '../lib/content';
2 import { absolutizeRelativeLinks } from '../lib/utils';
3
4 /**
5 * Raw-markdown companion endpoints for every prose page.
6 *
7 * For each content file at /{collection}/{slug} we also serve the authored
8 * markdown at /{collection}/{slug}.md. Consumers: LLM scrapers, the
9 * "Copy page content" button, anyone who wants the text form of a page.
10 *
11 * Scope: only prose collections. Excluded: the homepage, collection root
12 * landing pages (which have no authored body of their own), subfolder
13 * index pages, and data-driven routes.
14 */
15
16 const MD_COLLECTIONS = ['reference', 'patterns', 'essays', 'interviews', 'extensions'];
17 const STANDALONE_PAGES = ['about', 'docs'];
18
19 /**
20 * @typedef {Object} PathProps
21 * @property {string} filePath
22 */
23
24 export async function getStaticPaths() {
25 /** @type {Array<{ params: { slug: string }; props: PathProps }>} */
26 const paths = [];
27
28 for (const slug of STANDALONE_PAGES) {
29 const folder = await getFolder(slug);
30 if (folder.path) {
31 paths.push({ params: { slug }, props: { filePath: folder.path } });
32 }
33 }
34
35 for (const collection of MD_COLLECTIONS) {
36 const folder = await getFolder(collection);
37 for (const file of folder.allFiles) {
38 paths.push({
39 params: { slug: `${collection}/${file.slug}` },
40 props: { filePath: file.path },
41 });
42 }
43 }
44
45 return paths;
46 }
47
48 /** @type {import('astro').APIRoute} */
49 export const GET = async ({ props, site }) => {
50 const { filePath } = /** @type {PathProps} */ (props);
51 const origin = site?.origin || 'https://htmx.org';
52 const content = absolutizeRelativeLinks(fileAsMarkdown(filePath), origin);
53
54 return new Response(content, {
55 headers: {
56 'Content-Type': 'text/markdown; charset=utf-8',
57 'Cache-Control': 'public, max-age=3600',
58 },
59 });
60 };